﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameStateManagement;
using Microsoft.Xna.Framework.Content;

namespace DrWileyO_REDACTED_Adventure.Screens
{
    abstract class MenuScreen : GameScreen
    {
        List<MenuEntry> menuEntries = new List<MenuEntry>();
        int selectedEntry = 0;
        string menuTitle;

        InputAction menuUp;
        InputAction menuDown;
        InputAction menuSelect;
        InputAction menuCancel;


        protected IList<MenuEntry> MenuEntries
        {
            get { return menuEntries; }
        }

        public MenuScreen(string menuTitle)
        {
            this.menuTitle = menuTitle;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);

            menuUp = new InputAction(
                new Buttons[] { Buttons.DPadUp, Buttons.LeftThumbstickUp },
                new Keys[] { Keys.Up , Keys.W},
                true);
            menuDown = new InputAction(
                new Buttons[] { Buttons.DPadDown, Buttons.LeftThumbstickDown },
                new Keys[] { Keys.Down, Keys.S },
                true);
            menuSelect = new InputAction(
                new Buttons[] { Buttons.A, Buttons.Start },
                new Keys[] { Keys.Enter, Keys.Space },
                true);
            menuCancel = new InputAction(
                new Buttons[] { Buttons.B, Buttons.Back },
                new Keys[] { Keys.Escape },
                true);
        }

        public override void HandleInput(GameTime gameTime, InputState input)
        {
            // For input tests we pass in our ControllingPlayer, which may
            // either be null (to accept input from any player) or a specific index.
            // If we pass a null controlling player, the InputState helper returns to
            // us which player actually provided the input. We pass that through to
            // OnSelectEntry and OnCancel, so they can tell which player triggered them.
            PlayerIndex playerIndex;
            
            // Move to the previous menu entry?
            if (menuUp.Evaluate(input, ControllingPlayer, out playerIndex))
            {
                SoundManager.Instance.playSound(SoundManager.Sound.menuBlip);

                selectedEntry--;

                if (selectedEntry < 0)
                    selectedEntry = menuEntries.Count - 1;
            }

            // Move to the next menu entry?
            if (menuDown.Evaluate(input, ControllingPlayer, out playerIndex))
            {
                SoundManager.Instance.playSound(SoundManager.Sound.menuBlip);
                selectedEntry++;

                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;
            }

            if (menuSelect.Evaluate(input, ControllingPlayer, out playerIndex))
            {
                OnSelectEntry(selectedEntry, playerIndex);
            }
            else if (menuCancel.Evaluate(input, ControllingPlayer, out playerIndex))
            {
                OnCancel(playerIndex);
            }
        }

        protected virtual void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {
            menuEntries[entryIndex].OnSelectEntry(playerIndex);
        }

        protected virtual void OnCancel(PlayerIndex playerIndex)
        {
            ExitScreen();
        }

        protected void OnCancel(object sender, PlayerIndexEventArgs e)
        {
            OnCancel(e.PlayerIndex);
        }

        protected virtual void UpdateMenuEntryLocations()
        {
            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // start at Y = 175; each X value is generated per entry
            Vector2 position = new Vector2(0f, 175f);

            // update each menu entry's location in turn
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuEntry menuEntry = menuEntries[i];
                /**
                // each entry is to be centered horizontally
                position.X = ScreenManager.GraphicsDevice.Viewport.Width / 2 - menuEntry.GetWidth(this) / 2;

                if (ScreenState == ScreenState.TransitionOn)
                    position.X -= transitionOffset * 256;
                else
                    position.X += transitionOffset * 512;

                // set the entry's position
                menuEntry.Position = new Vector2(350, 400);

                // move down for the next entry the size of this entry
                position.Y += menuEntry.GetHeight(this); */

                menuEntry.Position = new Vector2(325, 400 + i * 50);

            }
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            // Update each nested MenuEntry object.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);

                menuEntries[i].Update(this, isSelected, gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ContentManager content = new ContentManager(ScreenManager.Game.Services, "Content");
            Texture2D splash = content.Load<Texture2D>("splashscreenwithouttxt");
            Texture2D square = content.Load<Texture2D>("alphaSquare");
            // make sure our entries are in the right place before we draw them
            UpdateMenuEntryLocations();

            

            GraphicsDevice graphics = ScreenManager.GraphicsDevice;
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = content.Load<SpriteFont>("MainMenuFont");

            spriteBatch.Begin();

            spriteBatch.Draw(splash, Vector2.Zero, Color.White);
            // THE *MOST* HACKY PART OF OUR CODE
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.Draw(square, new Rectangle(90, 50, 590, 100), null, Color.Black, 0.0f, Vector2.Zero, SpriteEffects.None, 0f);
            spriteBatch.DrawString(font, "SCIENCE 101", new Vector2(100, 30), Color.White);


           // menuEntry.Position = new Vector2(325, 400 + i * 50);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);
            spriteBatch.Draw(square, new Rectangle(305, 380, 230, 100), Color.Black);

            // Draw each menu entry in turn.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuEntry menuEntry = menuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, isSelected, gameTime);
            }


            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(graphics.Viewport.Width / 2, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = Color.Blue * TransitionAlpha;
            float titleScale = 2f;

            titlePosition.Y -= transitionOffset * 100;


            spriteBatch.End();
        }
    }
}
